﻿var web960 = {};
web960.debug = function (msg) {
    if (console) {
        console.log(msg);
    }
    else {
        alert("debug:" + msg);
    }
};
web960.utils = {};
web960.utils.tools = {
    nano: function (template, data) {
        return template.replace(/\{([\w\.]*)\}/g, function (str, key) {
            var keys = key.split("."), v = data[keys.shift()];
            for (var i = 0, l = keys.length; i < l; i++) {
                if (typeof v !== "undefined" && v !== null) { v = v[keys[i]]; }
                else {
                    v = "";
                }
            }
            return (typeof v !== "undefined" && v !== null) ? v : "";
        })
    },
    getvalfromjson: function (v, template, defval) {
        //v = v.replace(/\n/, "\\n");
        var rv = "";
        if (typeof v !== "undefined" && v !== null) {
            if (typeof v == "string") {
                v = JSON.parse(v);
            }
            rv = web960.utils.tools.nano(template, v);
        }
        else {
            if (defval != undefined) {
                rv = defval;
            }
        }
        return rv;
    },
    sort: function (order, sortBy) {
        var ordAlpah = (order == 'asc') ? '>' : '<';
        var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');
        return sortFun;
    }
};
web960.utils.select = function () {
    this.Config = {}; var namdeom = "";
    this.Init = function (el, cfg, nam) {
        this.Config = $.extend(this.Config, cfg);
        namdeom = "item";
        if (nam != undefined)
        {
            namdeom = nam;
        }
        var defcfg = this.Config;
        var _this = this;
        var sall = $('<a href="javascript:void(0);">全选</a>').click(function () {
            _this.selAll(true);
        });
        var scancel = $('<a href="javascript:void(0);">取消</a>').click(function () {
            _this.selAllcel();
            //            $("input[type='checkbox'][name='" + namdeom + "']", defcfg.el).attr("checked", false);
            //            if (defcfg.oncel) {
            //                defcfg.oncel($("input[type='checkbox'][name='" + namdeom + "']", defcfg.el));
            //            }
        });
        var sno = $('<a href="javascript:void(0);">反选</a>').click(function () {
            $("input[type='checkbox'][name='" + namdeom + "']", defcfg.el).each(function () {
                if ($(this).attr("checked")) {
                    $(this).attr("checked", false);
                    if (defcfg.oncel) { defcfg.oncel($(this)); };
                }
                else {
                    $(this).attr("checked", true);
                    if (defcfg.oncel) { defcfg.onsel($(this)); };
                }
            });
        });
        var im = $([]).add($('<strong>选择：</strong>')).add(sall).add($('<font> - </font>')).add(scancel).add($('<font> - </font>')).add(sno);
        $(el).empty().append(im);
        $("input[type='checkbox'][name='" + namdeom + "']", defcfg.el).live("click", function () {
            if ($(this).attr("checked")) {
                if (defcfg.oncel) { defcfg.onsel($(this)); };
            }
            else {

                if (defcfg.oncel) { defcfg.oncel($(this)); };
            }
        });
        if (defcfg.lineel) {
            $(defcfg.lineel).live("click", function (event) {
                var tr = event.srcElement ? event.srcElement : event.target;
                if (tr.tagName.toUpperCase() == "TD") {
                    var o = $("input[type='checkbox'][name='" + namdeom + "']", $(this));
                    if (o.attr("checked")) {
                        o.attr("checked", false);
                        if (defcfg.oncel) { defcfg.oncel(o); };
                    }
                    else {
                        o.attr("checked", true);
                        if (defcfg.oncel) { defcfg.onsel(o); };
                    }
                }
            });
        }
    };
    this.cancelSel = function (v, callback) {
        var o = $("input[type='checkbox'][name='" + namdeom + "'][value='" + v + "']", this.Config.el);
        o.attr("checked", false);
        if (this.Config.oncel) { this.Config.oncel(o); };
        callback();
    };
    this.selAll = function (v) {

        $("input[type='checkbox'][name='" + namdeom + "']", this.Config.el).attr("checked", v);
        if (this.Config.onsel && v) {
            this.Config.onsel($("input[type='checkbox'][name='" + namdeom + "']", this.Config.el));
        }
    };
    this.selAllcel = function () {
        $("input[type='checkbox'][name='" + namdeom + "']", this.Config.el).attr("checked", false);
        if (this.Config.oncel) {
            this.Config.oncel($("input[type='checkbox'][name='" + namdeom + "']", this.Config.el));
        }
    };
    this.getVal = function (callback) {
        var defcfg = this.Config;
        var strv = "";
        var arrayv = [];
        $("input[type='checkbox'][name='" + namdeom + "']:checked", defcfg.el).each(function () {
            strv += $(this).val() + ",";
            arrayv.push($(this).val());
        });
        strv = strv == "" ? "" : strv.substr(0, strv.length - 1);
        callback(strv, arrayv);
    };
};
web960.page = {
    Init: function (el, cfg, loaddata) {
        var defcfg = {
            totalcount: 0,
            page: 1,
            psize: 20
        };
        defcfg = $.extend(defcfg, cfg);
        var pagecount = Math.ceil(defcfg.totalcount / defcfg.psize);
        var firstpage = $('<a href="javascript:void(0);">首页</a>').click(function () {
            defcfg.page = 1;
            loaddata(defcfg);
        });
        var prevpage = $('<a href="javascript:void(0);">上一页</a>').click(function () {
            defcfg.page = (defcfg.page - 1);
            loaddata(defcfg);
        });
        var nextpage = $('<a href="javascript:void(0);">下一页</a>').click(function () {
            defcfg.page = (defcfg.page + 1);
            loaddata(defcfg);

        });
        var lastpage = $('<a href="javascript:void(0);">末页</a>').click(function () {
            defcfg.page = pagecount;
            loaddata(defcfg);
        });
        var gotopage = $('<input type="input" size="4" style="width:20px;" />').val(defcfg.page).on("keypress", function () {
            if (event.keyCode == 13) {
                var type = "^[0-9]*[1-9][0-9]*$";
                var ss = $.trim($(this).val());
                var re = new RegExp(type);
                if (ss.match(re) == null) {
                    $(this).val("");
                }
                else {
                    if (parseInt(ss) <= pagecount) {
                        defcfg.page = parseInt(ss);
                        loaddata(defcfg);
                    }
                    else {
                        $(this).val("");
                    }
                }
            }
        });

        var psizelist = [10, 20, 50, 100];
        var psizeSelect = $('<select></select>');
        if (jQuery.inArray(parseInt(defcfg.psize), psizelist) == -1) {
            psizeSelect.append('<option selected="selected">' + defcfg.psize + '</option>');
        }
        for (var i = 0; i < psizelist.length; i++) {
            if (defcfg.psize == psizelist[i]) {
                psizeSelect.append('<option selected="selected">' + psizelist[i] + '</option>');
            }
            else {
                psizeSelect.append('<option>' + psizelist[i] + '</option>');
            }
        }
        psizeSelect.on("change", function () {
            defcfg.page = 1;
            defcfg.psize = parseInt($(this).val());
            loaddata(defcfg);
        })
        var pagerbox = $('<div class="pagenavbox"></div>');

        var al;
        if (pagecount > 1) {
            if (defcfg.page == 1) {
                al = $([]).add('<span>共' + defcfg.totalcount + '条</span>').add('<span>转到</span>').add(gotopage).add('<span>页 共' + pagecount + '页</span>').add('<span>每页显示</span>').add(psizeSelect).add(nextpage).add(lastpage);
            }
            else if (defcfg.page < pagecount) {
                al = $([]).add('<span>共' + defcfg.totalcount + '条</span>').add('<span>转到</span>').add(gotopage).add('<span>页 共' + pagecount + '页</span>').add('<span>每页显示</span>').add(psizeSelect).add(firstpage).add(prevpage).add(nextpage).add(lastpage);
            }
            else if (defcfg.page == pagecount) {
                al = $([]).add('<span>共' + defcfg.totalcount + '条</span>').add('<span>转到</span>').add(gotopage).add('<span>页 共' + pagecount + '页</span>').add('<span>每页显示</span>').add(psizeSelect).add(firstpage).add(prevpage);
            }
        }
        else {
            al = $([]).add('<span>共' + defcfg.totalcount + '条</span>');
        }
        $(el).empty().append(pagerbox.append(al));

    }
};
